package com.wlc.controller;

import com.wlc.pojo.EmployeeStats;
import com.wlc.pojo.Result;
import com.wlc.service.EmployeeStatsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/stats/employee")
@Slf4j
public class EmployeeStatsController {

    @Autowired
    private EmployeeStatsService employeeStatsService;

    @GetMapping("/dept")
    public Result<List<EmployeeStats>> getDeptStats() {
        log.info("获取部门员工统计");
        try {
            List<EmployeeStats> stats = employeeStatsService.getDeptEmployeeStats();
            return Result.success(stats);
        } catch (Exception e) {
            log.error("获取部门员工统计失败: {}", e.getMessage());
            return Result.error("获取部门员工统计失败");
        }
    }

    @GetMapping("/gender")
    public Result<List<EmployeeStats>> getGenderStats() {
        log.info("获取性别统计");
        try {
            List<EmployeeStats> stats = employeeStatsService.getGenderStats();
            return Result.success(stats);
        } catch (Exception e) {
            log.error("获取性别统计失败: {}", e.getMessage());
            return Result.error("获取性别统计失败");
        }
    }

    @GetMapping("/job-level")
    public Result<List<EmployeeStats>> getJobLevelStats() {
        log.info("获取职级统计");
        try {
            List<EmployeeStats> stats = employeeStatsService.getJobLevelStats();
            return Result.success(stats);
        } catch (Exception e) {
            log.error("获取职级统计失败: {}", e.getMessage());
            return Result.error("获取职级统计失败");
        }
    }

    @GetMapping("/work-state")
    public Result<List<EmployeeStats>> getWorkStateStats() {
        log.info("获取在职状态统计");
        try {
            List<EmployeeStats> stats = employeeStatsService.getWorkStateStats();
            return Result.success(stats);
        } catch (Exception e) {
            log.error("获取在职状态统计失败: {}", e.getMessage());
            return Result.error("获取在职状态统计失败");
        }
    }

    @GetMapping("/monthly-trend")
    public Result<List<EmployeeStats>> getMonthlyTrend(@RequestParam(required = false) String year) {
        log.info("获取月度入职离职趋势: year={}", year);
        try {
            List<EmployeeStats> stats = employeeStatsService.getMonthlyHireLeaveStats(year);
            return Result.success(stats);
        } catch (Exception e) {
            log.error("获取月度趋势失败: {}", e.getMessage());
            return Result.error("获取月度趋势失败");
        }
    }

    @GetMapping("/summary")
    public Result<EmployeeStats> getSummary(@RequestParam(required = false) String year,
                                            @RequestParam(required = false) String month) {
        log.info("获取员工汇总统计: year={}, month={}", year, month);
        try {
            EmployeeStats summary = employeeStatsService.getEmployeeSummary(year, month);
            return Result.success(summary);
        } catch (Exception e) {
            log.error("获取员工汇总统计失败: {}", e.getMessage());
            return Result.error("获取员工汇总统计失败");
        }
    }

    @GetMapping("/overview")
    public Result<Object> getOverview(@RequestParam(required = false) String year,
                                      @RequestParam(required = false) String month) {
        log.info("获取员工统计总览: year={}, month={}", year, month);
        try {
            Object overview = employeeStatsService.getAllEmployeeStats(year, month);
            return Result.success(overview);
        } catch (Exception e) {
            log.error("获取员工统计总览失败: {}", e.getMessage());
            return Result.error("获取员工统计总览失败");
        }
    }
}